package com.example.boxueguapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.util.MD5Util;

public class LoginActivity extends AppCompatActivity {
    private RelativeLayout appBar;
    private ImageView back;
    private TextView titleBar;
    private ImageView imageView;
    private EditText username;
    private EditText pwd;
    private Button LoginButton;
    private Button zuce;
    private Button wangji;
    SharedPreferences userinfo;
    private String userval;
    private String pwdval;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        appBar = findViewById(R.id.app_bar);
        back = (ImageView) findViewById(R.id.back);
        titleBar = (TextView) findViewById(R.id.title_bar);
        imageView = (ImageView) findViewById(R.id.imageView);
        username = (EditText) findViewById(R.id.username);
        pwd = (EditText) findViewById(R.id.pwd);
        LoginButton = (Button) findViewById(R.id.Login_Button);
        zuce = (Button) findViewById(R.id.zuce);
        wangji = (Button) findViewById(R.id.wangji);

        appBar.setBackgroundColor(Color.TRANSPARENT);
        titleBar.setText("登录");
        back.setOnClickListener(v -> finish());
        zuce.setOnClickListener(v -> startActivityForResult(new Intent(LoginActivity.this, RegisterActivity.class), 1));
        LoginButton.setOnClickListener(v -> {
            userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
            userval = username.getText().toString();
            pwdval = pwd.getText().toString();
            if (isUserNameNull()) {
                Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            } else if (isUserName()) {
                Toast.makeText(LoginActivity.this, "用户名不存在", Toast.LENGTH_SHORT).show();
            } else if (isPwd()) {
                Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            } else if (isPwdT()) {
                Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                SharedPreferences.Editor edit = userinfo.edit();
                edit.putBoolean("isLogin", true);
                edit.putString("LoginUser", userval);
                edit.apply();
                Intent intent = new Intent();
                intent.putExtra("LoginUser", userval);
                setResult(2, intent);
                finish();
            }
        });
    }

    private boolean isUserNameNull() {
        return TextUtils.isEmpty(userval);
    }

    private boolean isUserName() {
        return TextUtils.isEmpty(userinfo.getString(userval, null));
    }

    private boolean isPwd() {
        return TextUtils.isEmpty(pwdval);
    }

    private boolean isPwdT() {
        return !userinfo.getString(userval, null).equals(MD5Util.md5(pwdval));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1 && data != null) {
            userval = data.getStringExtra("username");
            this.username.setText(userval);
        }
    }
}